package com.arvato.wxwork.controller.admin.store;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.store.WxworkAreaDo;
import com.arvato.wxwork.bean.bean_vo.store.WxworkAreaVo;
import com.arvato.wxwork.service.store.WxworkAreaService;

/**
 * 企业微信销售地区管理Controller
 * @author zhan655
 */
@Controller
@RequestMapping("/admin/wxwork/area/")
public class WxworkAreaController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkAreaController.class);

	@Autowired
	private FtpTool ftpTool;
	
	@Autowired
	private WxworkAreaService wxworkAreaService;

	/**
	 * 查询区域列表
	 * @param req
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData listArea(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		PageData pageData = wxworkAreaService.listAreaByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 根据ID查询一个区域的详情
	 * @param req
	 * @param areaId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result getAreaById(HttpServletRequest req, String areaId) {
		WxworkAreaVo areaVo = wxworkAreaService.getAreaById(areaId);
		if(areaVo != null) {
			return Result.success(areaVo);
		}
		return Result.fail("查询出错"); 
	}
	
	/**
	 * 插入区域信息
	 * @param req
	 * @param wxworkAreaDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result insert(HttpServletRequest req, WxworkAreaDo wxworkAreaDo, @RequestParam("file") MultipartFile file) {
		// 新增之前 首先要检查新增大区的下属省份有没有出现在其他大区里面
		String tips = wxworkAreaService.check(wxworkAreaDo.getProvinceList(), wxworkAreaDo.getAreaId());
		if(tips != null) {
			return Result.fail("新增失败：" + tips);
		}
		String fileName = ftpTool.uploadFile(file);
		System.out.println("文件名：" + fileName);
		if(fileName == null) {
			logger.info("图片上传失败");
			return Result.fail("图片上传失败");
		}
		wxworkAreaDo.setFileName(fileName);
		try {
			int row = wxworkAreaService.insert(wxworkAreaDo);
			if(row == 1) {
				return Result.success("新增成功");
			}
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,地区编号:" + wxworkAreaDo.getAreaCode() + "已存在");
		} catch (Exception e) {
			logger.info("新增区域出错", e);
			return Result.fail(e.getMessage());
		}
		return Result.fail("新增区域出错");
	}
	
	/**
	 * 修改区域信息
	 * @param req
	 * @param wxworkAreaDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(HttpServletRequest req, WxworkAreaDo wxworkAreaDo, @RequestParam("file") MultipartFile file) {
		// 新增之前 首先要检查新增大区的下属省份有没有出现在其他大区里面
		String tips = wxworkAreaService.check(wxworkAreaDo.getProvinceList(), wxworkAreaDo.getAreaId());
		if(tips != null) {
			return Result.fail("新增失败：" + tips);
		}
		String fileName = ftpTool.uploadFile(file);
		System.out.println("文件名：" + fileName);
		if(fileName == null) {
			logger.info("图片上传失败");
			return Result.fail("图片上传失败");
		}
		wxworkAreaDo.setFileName(fileName);
		try {
			int row = wxworkAreaService.update(wxworkAreaDo);
			if(row == 1) {
				return Result.success("修改成功");
			}
		} catch (DuplicateKeyException e) {
			return Result.fail("更新失败,地区编号已存在：" + wxworkAreaDo.getAreaCode());
		}catch (Exception e) {
			logger.info("修改区域出错", e);
			return Result.fail(e.getMessage());
		}
		return Result.fail("修改区域出错");
	}

	/**
	 * 删除区域
	 * @param req
	 * @param areaId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(HttpServletRequest req, String areaId) {
		try {
			int row = wxworkAreaService.delete(areaId);
			if(row == 1) {
				return Result.success("删除成功");
			}
		} catch (Exception e) {
			logger.info("删除区域出错", e);
			return Result.fail(e.getMessage());
		}
		return Result.fail("删除区域出错");
	}
	
}

